package com.example.myutil;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class HttpReponseUtil {

	private static Logger logger = LoggerFactory.getLogger(HttpReponseUtil.class);
	
	public final static void writeJson(HttpServletResponse response, String json){
		response.setContentType("application/json;charset=UTF-8");
		write(response, json);
	}
	
	/**
	 * <b>作者:</b> Bob<br/>
	 * <b>修改时间：</b>2016年5月19日 - 上午10:12:51<br/>
	 * <b>功能说明：</b>	向客户端输出html<br/>
	 * @param response
	 * @param str
	 */
	public final static void writeHtml(HttpServletResponse response, String str){
		response.setContentType("text/html;charset=UTF-8");
		write(response, str);
	}
	
	/**
	 * <b>作者:</b> Bob<br/>
	 * <b>修改时间：</b>2016年5月19日 - 上午10:12:51<br/>
	 * <b>功能说明：</b>	向客户端输出html<br/>
	 * @param response
	 * @param str
	 */
	public final static void writeJavascript(HttpServletResponse response, String script){
		response.setContentType("text/javascript;charset=UTF-8");
		write(response, script);
	}
	
	private final static void write(HttpServletResponse response, String str){
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write(str);
			pw.flush();
		} catch (IOException e) {
			logger.error("发生IO异常!");
		}
	}
	
}
